<?php
namespace App\Http\Controllers\Api\Trade\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Trade\Shop\ItemCollectionRequest;
use App\Http\Resources\Trade\Shop\ListResource as ShopListResource;
use App\Services\Trade\Auth\ShopService;

class ShopController extends Controller
{

    /**
     * 收藏数据（一条）
     *
     * @param $id 必选            
     * @param $type 必选            
     */
    public function collectionItem(ItemCollectionRequest $request)
    {
        $data['id'] = $id = $request->input('id');
        $data['type'] = $type = $request->input('type');
        
        $shopService = new ShopService();
        $shopService->collectionItem($id, $type, $this->identity()->getUserId());
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 批量删除我的关注列表
     *
     * @param $ids 必选            
     */
    public function deleteCollection(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required|array|between:1,50'
        ]);
        $data['ids'] = $ids = $request->input('ids');
        $userId = $this->identity()->getUserId();
        
        $shopService = new ShopService();
        $shopService->deleteCollection($ids, $userId);
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 我的收藏列表
     *
     * @param $page 可选            
     * @param $page_size 可选            
     */
    public function getCollectionList(Request $request)
    {
        $this->validate($request, [
            'page' => 'nullable|integer|between:1,10000',
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $page = intval($request->query('page', 1));
        $pageSize = intval($request->query('page_size', 50));
        $userId = $this->identity()->getUserId();
        
        $shopService = new ShopService();
        list ($collection, $count) = $shopService->selectCollectionList($page, $pageSize, $userId);
        
        $data['count'] = [
            'total' => $count,
            'page' => $page,
            'page_size' => $pageSize
        ];
        $data['data'] = new ShopListResource($collection);
        
        return $this->response->setResponseData($data)->getResponse();
    }
}
